/*
 * Copyright (c) 2017 Solidwang. All Rights Reserved
 */
package com.solid4j.auth.service.impl;


import com.solid4j.auth.dao.BaseDao;
import com.solid4j.auth.model.Menu;
import com.solid4j.auth.service.MenuService;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: solidwang
 * @date: 17/5/4
 */
@Component("menuService")
public class MenuServiceImpl implements MenuService {

    @Resource
    private BaseDao<Menu> baseDao;

    @Override
    public Integer save(Menu menu) {
        return baseDao.save(menu);
    }

    @Override
    public List<Menu> getMenuListByParentId(Integer parentId) {
        List<Menu> menus = new ArrayList<Menu>();
        try {
            StringBuffer hql = new StringBuffer();
            hql.append("from Menu m").append(" where m.parentId = ").append(parentId).append(" order by menuOrder");
            menus = baseDao.getList(hql.toString());
            return menus;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return menus;
    }

    @Override
    public Menu get(Integer id) {
        return baseDao.get(Menu.class, id);
    }

    @Override
    public void update(Menu menu) {
        baseDao.update(menu);
    }

    @Override
    public void delete(Menu menu) {
        baseDao.delete(menu);
    }

    @Override
    public List<Menu> getMenus() {
        List<Menu> list = new ArrayList<Menu>();
        try {
            String hql = "from Menu";
            list = baseDao.getList(hql);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
